package com.youedata.modular.controller;

import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youedata.base.auth.annotion.Permission;
import com.youedata.base.log.BussinessLog;
import com.youedata.base.pojo.page.LayuiPageFactory;
import com.youedata.base.tips.DCResponse;
import com.youedata.modular.entity.GivenPublicNotice;
import com.youedata.modular.entity.GoodsInfo;
import com.youedata.modular.model.dto.GoodsInfoDTO;
import com.youedata.modular.service.IGoodsInfoService;
import com.youedata.sys.core.constant.Const;
import com.youedata.sys.core.constant.dictmap.UserDict;
import com.youedata.sys.core.constant.state.ManagerStatus;
import com.youedata.sys.core.exception.enums.BizExceptionEnum;
import com.youedata.sys.core.util.UserHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 商品表控制器
 *
 * @author hst
 * @date 2021-08-05 07:33:14
 */
@Controller
@Api(value = "/goodsInfo", tags = "商品表管理")
@RequestMapping("/goodsInfo")
public class GoodsInfoController {

    private String PREFIX = "/goodsInfo";

    @Autowired
    private IGoodsInfoService goodsInfoService;

    /**
     * 新增
     *
     * @author hst
     * @date 2021-08-05
     */
    @ResponseBody
    @PostMapping("/addItem")
    @ApiOperation(value = "新增")
    public DCResponse<Void> addItem(GoodsInfoDTO goodsInfoDto) {
//        GoodsInfo goodsInfo = new GoodsInfo();
//        BeanUtils.copyProperties(goodsInfoDto, goodsInfo);
//        goodsInfo.setCreateUser(UserHolder.getUserId());
//        goodsInfo.setCreateTime(LocalDate.from(LocalDateTime.now()));
        this.goodsInfoService.saveSelective(goodsInfoDto);
        return DCResponse.ok(null);
    }

    /**
     * 根据主键删除
     *
     * @author hst
     * @date 2021-08-05
     */
    @ResponseBody
    @PostMapping("/delete")
    @ApiOperation("删除")
    public DCResponse<Void> delete(@RequestParam("id") String id) {
        this.goodsInfoService.removeById(id);
        return DCResponse.ok(null);
    }

    /**
     * 修改
     *
     * @author hst
     * @date 2021-08-05
     */
    @ResponseBody
    @PostMapping("/editItem")
    @ApiOperation("根据ID修改信息")
    public DCResponse<Void> editItem(GoodsInfoDTO goodsInfoDto) {
        this.goodsInfoService.updateGoodsInfo(goodsInfoDto);
        return DCResponse.ok(null);
    }

    /**
     * 根据主键查询单条详情
     *
     * @author hst
     * @date 2021-08-05
     */
    @ResponseBody
    @PostMapping("/detail")
    @ApiOperation(value = "根据主键查询单条详情")
    public ResponseData detail(@RequestParam("id") String id) {

        return ResponseData.success(this.goodsInfoService.detailById(id, "N"));
    }

    /**
     * 修改商品状态
     *
     * @author hao.yan
     * @Date 2021/08/25 22:44
     */
    @ResponseBody
    @RequestMapping("/changeAttribute")
    @ApiOperation(value = "修改商品状态")
//    @Permission(Const.ADMIN_NAME)
    public ResponseData changeAttribute(Long id, Boolean flag) {
        GoodsInfo goodsInfo = this.goodsInfoService.getById(id);
        if (com.youedata.util.ToolUtil.isNotEmpty(goodsInfo)) {
            goodsInfo.setAttribute(flag ? "热门商品" : "非热门商品");
        }
        this.goodsInfoService.updateById(goodsInfo);
        return ResponseData.success(goodsInfo);
    }

    /**
     * 分页查询列表
     *
     * @author hst
     * @date 2021-08-05
     */
    @ResponseBody
    @GetMapping("/list")
    @ApiOperation(value = "分页查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", required = true, defaultValue = "1", paramType = "query", dataType = "int", example = "1"),
            @ApiImplicitParam(name = "limit", value = "每页多少条数据", required = true, defaultValue = "10", paramType = "query", dataType = "int", example = "20")
    })
    public Object list(GoodsInfoDTO goodsInfoDto) {
        GoodsInfo goodsInfo = new GoodsInfo();

        //过滤空字符串
        goodsInfoDto.setName(StringUtils.isEmpty(goodsInfoDto.getName()) ? null : goodsInfoDto.getName());
        goodsInfoDto.setType(StringUtils.isEmpty(goodsInfoDto.getType()) ? null : goodsInfoDto.getType());
        goodsInfoDto.setRegion(StringUtils.isEmpty(goodsInfoDto.getRegion()) ? null : goodsInfoDto.getRegion());

        BeanUtils.copyProperties(goodsInfoDto, goodsInfo);

        //获取分页参数
        Page page = LayuiPageFactory.defaultPage();

        //根据条件查询
        //page.setRecords(this.goodsInfoService.page(page, Wrappers.query(goodsInfo)).getRecords());
        page.setRecords(this.goodsInfoService.queryFuzzySearch(page, goodsInfo));
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 分页查询列表
     *
     * @author hst
     * @date 2021-08-05
     */
    @ResponseBody
    @GetMapping("/sort")
    @ApiOperation(value = "排序选择热门商品")
    public Object sort(GoodsInfoDTO goodsInfoDto) {
        GoodsInfo goodsInfo = new GoodsInfo();

        BeanUtils.copyProperties(goodsInfoDto, goodsInfo);

        //获取分页参数
        Page page = LayuiPageFactory.defaultPage();

        //根据条件查询
        //page.setRecords(this.goodsInfoService.page(page, Wrappers.query(goodsInfo)).getRecords());
        page.setRecords(this.goodsInfoService.querySort(goodsInfo));
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 跳转到主页面
     *
     * @author hst
     * @Date 2021-08-05
     */
    @RequestMapping()
    public String index() {
        return PREFIX + "/goodsInfo.html";
    }

    /**
     * 新增页面
     *
     * @author hst
     * @Date 2021-08-05
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/goodsInfo_add.html";
    }

    /**
     * 编辑页面
     *
     * @author hst
     * @Date 2021-08-05
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/goodsInfo_edit.html";
    }

}
